package com.it.data_tree.tree.redBlackTree;

import static com.it.data_tree.tree.redBlackTree.Color.RED;

/**
 * @Author: Coke
 * @DateTime: 2023/05/01/21:23
 * @注释: TODO
 **/
public class RedBlackNode {
    public int key;
    public Object value;
    public RedBlackNode left;
    public RedBlackNode right;
    public RedBlackNode parent; // 父节点
    public Color color = RED; // 默认是红色
    
    public RedBlackNode () {
    }
    
    public RedBlackNode (int key) {
        this.key = key;
    }
    
    public RedBlackNode (int key, Object value) {
        this.key = key;
        this.value = value;
    }
    
    public RedBlackNode (int key, Object value, RedBlackNode parent) {
        this.key = key;
        this.value = value;
        this.parent = parent;
    }
    
    public RedBlackNode (int key, Object value, RedBlackNode left, RedBlackNode right, RedBlackNode parent, Color color) {
        this.key = key;
        this.value = value;
        this.left = left;
        this.right = right;
        this.parent = parent;
        this.color = color;
    }
    
    /**
     * @return boolean
     * @作者: Coke
     * @注释: 判断是否是左孩子
     * @DateTime: 2023/5/1 21:37
     */
    boolean isLeftChild () {
        return parent != null && parent.left == this;
    }
    
    /**
     * @return RedBlackNode
     * @作者: Coke
     * @注释: 获取到叔叔节点
     * @DateTime: 2023/5/1 21:39
     */
    RedBlackNode uncle () {
        if (parent == null || parent.parent == null) {
            return null;
        }
        if (parent.isLeftChild()) {
            return parent.parent.right;
        } else {
            return parent.parent.left;
        }
    }
    
    /**
     * @return RedBlackNode
     * @作者: Coke
     * @注释: 获取到兄弟节点
     * @DateTime: 2023/5/1 22:06
     */
    RedBlackNode sibling () {
        if (parent == null) {
            return null;
        }
        if (this.isLeftChild()) {
            return parent.right;
        } else {
            return parent.left;
        }
    }
    
    @Override
    public String toString () {
        return "RedBlackNode{" +
              "key=" + key +
              ", value=" + value +
              ", color=" + color +
              '}';
    }
}
